package com.atguigu.lease.web.app.controller.history;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.UserInfoService;
import com.atguigu.lease.web.app.vo.history.HistoryItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "浏览历史管理")
@RequestMapping("/app/history")
public class BrowsingHistoryController {

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "获取浏览历史")

    @GetMapping("pageItem")
    private Result<IPage<HistoryItemVo>> page(@RequestParam long current, @RequestParam long size,@RequestHeader("access_token") String token) {
        IPage<HistoryItemVo> page=new Page<>(current,size);
        Claims claims = JwtUtil.parseToken(token);
        String phone = claims.get("phone",String.class);
        LambdaQueryWrapper<UserInfo> a = new LambdaQueryWrapper<>();
        a.eq(UserInfo::getPhone,phone);
        UserInfo userInfo = userInfoService.getOne(a);
        Long userId = userInfo.getId();
        browsingHistoryService.queryHistory(page,userId);
        return Result.ok(page);
    }

}
